<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>地址列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card class="box-card">
            <el-row :gutter="20">
                <el-col :span="4">
                    <el-button type="primary" @click="addDialogVisible = true">添加地址</el-button>
                </el-col>
            </el-row>
             <!-- 表格 -->
             <el-table :data="addressList"  stripe >
                <el-table-column type="index"></el-table-column>
                <el-table-column label="地址" prop="address" :filters="addressFilters" :filter-method="filterHandler"></el-table-column>
                <!-- <el-table-column label="下级关联状态" prop="hasChild" :filters="isConnetedFilters" :filter-method="filterHandler"></el-table-column> --> -->
                <el-table-column label="下级关联状态">
                    <!-- 作用域插槽 -->
                    <template slot-scope="scope">
                        <!-- @change="stateChanged(scope.row)" -->
                        <el-switch v-model="scope.row.hasChild" disabled></el-switch>
                    </template> 
                </el-table-column>

                <el-table-column label="操作">
                     <!-- 作用域插槽  在其中放置一些button-->
                    <template slot-scope="scope">
                        <!-- <el-button type="primary" size="mini" plain @click="showDetail(scope.row.id)">详情</el-button> -->
                        <el-button type="warning" size="mini" plain @click="showEditDialog(scope.row.id)">修改</el-button>
                        <el-button type="danger" size="mini" plain @click="deletePeopleById(scope.row.id)">删除</el-button>
                    </template> 
                </el-table-column>

            </el-table>
        </el-card>
        <!-- 新增对话框 -->
        <el-dialog
            title="新增地址"
            :visible.sync="addDialogVisible"
            width="50%"
            @close="addDialogClosed">
            <!-- 对话框内容主体 -->
            <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
                <el-form-item label="地址" prop="address"> <!--prop 是在editFormRule中用的-->
                    <el-input v-model="addForm.address"></el-input>
                </el-form-item>
            </el-form>
            <!-- 对话框底部 -->
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addAddressInfo">确 定</el-button>
            </span>
        </el-dialog> 
        <!-- 修改对话框 -->
        <el-dialog
            title="修改地址"
            :visible.sync="editDialogVisible"
            width="50%"
            @close="editDialogClosed">
            <!-- 对话框内容主体 -->
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
                <el-form-item label="地址" prop="address"> <!--prop 是在editFormRule中用的-->
                    <el-input v-model="editForm.address"></el-input>
                </el-form-item>
            </el-form>
            <!-- 对话框底部 -->
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editAddressnfo">确 定</el-button>
            </span>
        </el-dialog> 
    </div>
</template>

<script>
    export default {
        data() {
            return {
                // 查询list参数
                queryInfo: {
                    pageNum:1,
                    pageSize:20,
                    query:''
                },
                
                editDialogVisible: false,
                addDialogVisible: false,
                // 事件列表总列表
                addressList: [
                    // {id: "172", address:"德国特里尔", hasChild:true},
                    // {id: "177", address:"德国巴门", hasChild:false}
                ],
                // 编辑对话框绑定数据
                editForm: {
                    address:""
                },
                addForm: {
                    address:""
                },
                // 编辑表单的验证规则对象
                editFormRules:{
                    address:[
                        { required: true, message: '请输入地址', trigger: 'blur' },
                        { min: 2, max: 50, message: '长度在50个字符', trigger: 'blur' }
                    ],
                },
                // 添加表单的验证规则对象
                addFormRules:{
                    address:[
                        { required: true, message: '请输入地址', trigger: 'blur' },
                        { min: 2, max: 50, message: '长度在50个字符', trigger: 'blur' }
                    ],
                },
                // 年份过滤内容
                addressFilters:[],
            }
        },
        created() {
            this.getAddressList();
        },
        methods: {
            // 展示编辑事件的对话框（修改）
            async showEditDialog(id) {
                this.editDialogVisible = true;
                const{data:res} = await this.$http.get("address/selectById", {params:{"id":id}});
                this.editForm = res.data;
            },
            // 修改对话框点击了确定事件
            editAddressnfo() {
                this.$refs.editFormRef.validate(async valid =>{
                    if (!valid) return
                    // 发起请求
                    const {data:res} = await this.$http.post("address/update", this.editForm);
                    if (res.code !== 200) {
                        return this.$message.error("更新失败");
                    }
                    // 关闭对话框
                    this.editDialogVisible = false;
                    // 刷新列表
                    this.getAddressList();
                    // 提示更新成功
                    this.$message.success("更新成功");
                } )
            },
            // 新增对话框点击了确定事件
            addAddressInfo() {
                this.$refs.addFormRef.validate(async valid =>{
                    if (!valid) return
                    // 发起请求
                    const {data:res} = await this.$http.post("address/insert", this.addForm);
                    if (res.code !== 200) {
                        return this.$message.error(res.meg);
                    }
                    // 关闭对话框
                    this.addDialogVisible = false;
                    // 刷新列表
                    this.getAddressList();
                    // 提示更新成功
                    this.$message.success("新增成功");
                } )
            },
            // 刷新事件列表
            async getAddressList() {
                const {data:res} = await this.$http.get("address/selectAll", {params: this.queryInfo})
                if (res.code != 200) return this.$message.error("获取时间列表失败!");
                this.addressList = res.data
                for (let i = 0; i < this.addressList.length; i++) {
                    const cur_address = this.addressList[i];

                    var addressObj = new Object();
                    addressObj.text = cur_address.year;
                    addressObj.value = cur_address.year;
                    this.addressFilters.push(addressObj);

                }
                // this.addressList = res.data.addressList;
                // this.total = res.data.total;
            },
            // 表格过滤函数
            filterHandler(value, row, column) {
                const property = column['property'];
                return row[property] === value;
            },
            // 编辑对话框关闭
            editDialogClosed() {
                this.$refs.editFormRef.resetFields();
            },
            addDialogClosed() {
                this.$refs.addFormRef.resetFields();
            },
            // 删除按钮事件
            async deletePeopleById(id) {
                // 弹框提示
                const conformRes = await this.$confirm('此操作将删除该条, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'  // 图标
                }).catch(err => {
                    return err
                }) 
                // 如果用户确认则返回confirm串 取消返回的是cancel
                // console.log(conformRes)
                if (conformRes !== "confirm") {
                    return this.$message.info("已经取消删除");
                }
                const{data:res} = await this.$http.post("address/deleteById", {id:id});
                if (res.code !== 200) {
                    return this.$message.error("删除失败");
                }
                this.$message.success("删除成功");
                this.getAddressList();                
            }
        }
    }
</script>

<style lang="less" scoped>
    .box-card {
        line-height: 0%;
        text-align: left;
    }
</style>